#include "CDoorControlBoardEditDlg.h"

CDoorControlBoardEditDlg::CDoorControlBoardEditDlg(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint, true);
    setAttribute(Qt::WA_AlwaysStackOnTop, true);

    // init
    ui.combo_portname->clear();
#ifdef Q_OS_WIN32
    for (int i = 1; i < 20; ++i)
    {
        ui.combo_portname->addItem(QString("COM%1").arg(i));
    }
#elif __arm__
    ui.combo_portname->addItem("/dev/ttymxc1");
    ui.combo_portname->addItem("/dev/ttymxc2");
    ui.combo_portname->addItem("/dev/ttymxc3");
    ui.combo_portname->addItem("/dev/ttymxc4");
#endif
}

void CDoorControlBoardEditDlg::SetData(const zl::DoorControlBoard &data)
{
    ui.edt_pcbno->setText(QString::number(data.col));
    ui.edt_count->setText(QString::number(data.count));
    ui.combo_protocol->setCurrentIndex(data.protocol_type - 1);
    ui.combo_datalink_type->setCurrentIndex(data.datalinkopt.type - 1);
    ui.ck_isshared->setChecked(data.datalinkopt.is_shared == 1);
    ui.edt_localport->setText(QString::number(data.datalinkopt.localport));
    ui.edt_remote_port->setText(QString::number(data.datalinkopt.remoteport));
    ui.edt_remote_addr->setText(data.datalinkopt.remoteaddr.c_str());
    ui.combo_portname->setCurrentText(data.datalinkopt.com.c_str());
    ui.edt_baudrate->setText(QString::number(data.datalinkopt.baud_rate));
    ui.edt_flow_control->setText(QString::number(data.datalinkopt.flow_control));
    ui.edt_parity->setText(QString::number(data.datalinkopt.parity));
    ui.edt_stopbits->setText(QString::number(data.datalinkopt.stop_bits));
    ui.edt_character_size->setText(QString::number(data.datalinkopt.character_size));
    ui.edt_timeout_conn->setText(QString::number(data.datalinkopt.timeout_conn));
    ui.edt_timeout_read->setText(QString::number(data.datalinkopt.timeout_read));
    ui.edt_timeout_write->setText(QString::number(data.datalinkopt.timeout_write));
}

void CDoorControlBoardEditDlg::on_btns_confirm_clicked()
{
    data_.col = ui.edt_pcbno->text().toInt();
    data_.count = ui.edt_count->text().toInt();
    data_.protocol_type = ui.combo_protocol->currentIndex() + 1;
    data_.datalinkopt.type = ui.combo_datalink_type->currentIndex() + 1;
    data_.datalinkopt.is_shared = ui.ck_isshared->isChecked() ? 1 : 0;
    data_.datalinkopt.localport = ui.edt_localport->text().toInt();
    data_.datalinkopt.remoteport = ui.edt_remote_port->text().toInt();
    data_.datalinkopt.remoteaddr = ui.edt_remote_addr->text().toStdString();
    data_.datalinkopt.com = ui.combo_portname->currentText().toStdString();
    data_.datalinkopt.baud_rate = ui.edt_baudrate->text().toInt();
    data_.datalinkopt.flow_control = ui.edt_flow_control->text().toInt();
    data_.datalinkopt.parity = ui.edt_parity->text().toInt();
    data_.datalinkopt.stop_bits = ui.edt_stopbits->text().toInt();
    data_.datalinkopt.character_size = ui.edt_character_size->text().toInt();
    data_.datalinkopt.timeout_conn = ui.edt_timeout_conn->text().toInt();
    data_.datalinkopt.timeout_read = ui.edt_timeout_read->text().toInt();
    data_.datalinkopt.timeout_write = ui.edt_timeout_write->text().toInt();

    accept();
}

void CDoorControlBoardEditDlg::on_btnd_cancel_clicked()
{
    reject();
}
